home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / pulldir.arc / PULLERS.C < prev   
C/C++ Source or Header  |  1991-04-21  |  9KB  |  363 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <memory.h>
  4. #include <dos.h>
  5. #define MAX 255
  6.  
  7. int field;
  8. int Highlight, Fg, Bg;
  9.  
  10. char *pulldir(unsigned char row, unsigned char col, char *spec)
  11. {
  12.     char *allkeys;
  13.     int selection;
  14.     unsigned int errcode;
  15.     /* don't work yet */
  16.     struct find_t b_file;
  17.     struct find_t *(c_file)[MAX];
  18.     char *fileitems[MAX];
  19.     int files, filenum, c;
  20.     static char sel_space[13];
  21.     static char *sel_name = sel_space;
  22.     /* if (strlen(spec)%2)
  23.         strcat(spec," ");
  24.     /* find first file in current directory   */
  25.  
  26.     filenum = 0;
  27.     c_file[filenum] = (struct find_t *)calloc(1, sizeof(struct find_t));
  28.     errcode = _dos_findfirst(spec, _A_NORMAL, &b_file);
  29.     if (errcode != 0)
  30.         return(NULL);
  31.  
  32.     memcpy(c_file[filenum], &b_file, sizeof(struct find_t));
  33.  
  34.     filenum++;
  35.     c_file[filenum] = (struct find_t *)calloc(1, sizeof(struct find_t));
  36.     while(_dos_findnext(&b_file) == 0) {
  37.         c_file[filenum] = (struct find_t *)calloc(1, sizeof(struct find_t));
  38.         memcpy(c_file[filenum], &b_file, sizeof(struct find_t));
  39.         filenum++;
  40.     }
  41.     filenum--;
  42.  
  43.     sortfiles(c_file, filenum+1);
  44.     allkeys = (char *)calloc(filenum,sizeof(char));
  45.  
  46.     /*     */
  47.     for (files = 0; files <= filenum; files++) {  /* others have files <= */
  48.         fileitems[files] = (char *)calloc(1, 13);
  49.         fileitems[files] = c_file[files]->name;
  50.         allkeys[files] = c_file[files]->name[0];
  51.     }
  52.     /* sort files  */
  53.     selection = pulldown(row, col, spec, fileitems, allkeys, filenum+1, 16);
  54.     if (selection == 255)
  55.         return(NULL);
  56.     /* */
  57.     printf("\nselected file %s is %ld bytes long",
  58.         c_file[selection]->name, c_file[selection]->size);
  59.     return(1);
  60.     /* */
  61.     strcpy(sel_name,c_file[selection]->name);
  62.     for (files = 0; files <= filenum; files++) {
  63.         free(fileitems[files]);
  64.         free(c_file[files]);
  65.     }
  66.     return(sel_name);
  67. }
  68.  
  69. int pulldown(char row, char col, const char *header, const char *prompt[], const char *keys, int lines, int width)
  70. {
  71. /* ========================================================================
  72.  *            Displays a pull down menu and returns with selection number
  73.  * ===================================================================== */
  74. char leftcol, rightcol, ch, spaces;
  75. extern Fg, Bg;
  76. int count, keycode, txtpos, lastpos, maxpos,insert = 1;
  77. unsigned char field;     /* declared here to over-ride extern use  */
  78. char *genp = "none";
  79. char bottom[80];
  80. unsigned c, scroll=0, lastscroll, showlines;
  81.  
  82.     /* align header and width */
  83.     if (!(strlen(header)%2))
  84.         strcat(header,"═");
  85.     if(width%2) width++;
  86.  
  87.     reverse(0);
  88.     leftcol = col+strlen(prompt[field])+2;
  89.  
  90.     if (row < 3)
  91.         row = 3;
  92.     if (col < 3)
  93.         col = 3;
  94.     if (row > 20)
  95.         row = 20;
  96.     if ((col+width) > 78)
  97.         col = 78 - width;
  98.     if (lines < 22-row)
  99.         showlines = lines;
  100.     else showlines = 22-row;
  101.  
  102.     /* make box for menu bar   */
  103.     locate(row, col);
  104.     printf("╒");
  105.     spaces = ((width - strlen(header))/2) - 1;
  106.     if (spaces > 0)
  107.         for (;(spaces); spaces--)
  108.             putch('═');
  109.     printf("%s", header);
  110.     spaces = ((width - strlen(header))/2) - 1;
  111.     if (spaces > 0)
  112.         for (;(spaces); spaces--)
  113.             putch('═');
  114.     printf("╕");
  115.     row++;
  116.  
  117.     for (spaces = 0; spaces < (width-2); spaces++)
  118.         bottom[spaces] = '─';
  119.     bottom[width-3] = '\0';
  120.     locate(row+showlines, col);
  121.     printf("└%s┘▒",bottom);
  122.     for (spaces = 0; spaces < (width-2); spaces++)
  123.         bottom[spaces] = '▒';
  124.     bottom[width-3] = '\0';
  125.     locate(row+showlines+1, col+1);
  126.     printf("%s▒▒",bottom);
  127.  
  128.     /* initial display of fields  */
  129.     for (count = 0; count < showlines; count++) {
  130.         locate(row+count, col);
  131.         spaces = (width - strlen(prompt[count]) - 3);
  132.         printf("│%s",prompt[count]);
  133.         for (;(spaces); spaces--)
  134.             putch(' ');
  135.         putch('│');
  136.         putch('▒');
  137.     }
  138.     col++;
  139.  
  140.  
  141.     for (field=0; field < showlines;) {
  142.         reverse(1);
  143.         locate(row+field,col);
  144.         printf("%s", prompt[field+scroll]);
  145.         locate(row+field,col);
  146.         fflush(stdin);
  147.         while(!kbhit())
  148.             ;
  149.         if ((keycode = getch()) != 00)
  150.             switch (keycode) {
  151.                 case 13:
  152.                     reverse(0);
  153.                     locate (23,1);
  154.                     return(field+scroll);
  155.                 case 27:
  156.                     /* no change to original string */
  157.                     reverse(0);
  158.                     locate (23,1);
  159.                     return(255);
  160.                 default:
  161.                     keycode = toupper(keycode);
  162.                     if(( c = choices(keycode,keys) ) != 255) {
  163.                         reverse(0);
  164.                         locate(row+field,col);
  165.                         printf("%s",prompt[field+scroll]);
  166.                         if(keys[c] == keys[scroll+field])      /* if same letter, call again */
  167.                             if((c = choices(keycode,&keys[scroll+field+1])) == 255)
  168.                                 /* if no more for same letter, restore value of c  */
  169.                                 c = scroll+field;
  170.                             else
  171.                                 /* add c to last offset */
  172.                                 c = c + scroll + field + 1;
  173.                         if(c > scroll+field) {
  174.                             /* if lower on list  */
  175.                             field = c - scroll;
  176.                             if (field > (showlines - 1)) {
  177.                                 /* if not in current scroll window   */
  178.                                 scroll = c;
  179.                                 field = 0;
  180.                             }
  181.                             if (scroll > (lines - showlines)) {
  182.                                 /* if in last scroll window */
  183.                                 scroll = lines - showlines;
  184.                                 field = (c - (lines - showlines));
  185.                             }
  186.                         }
  187.                         else {
  188.                             /* if lower on list */
  189.                             field = c - scroll;
  190.                             if (c < scroll) {
  191.                                 /* if not in current scroll window   */
  192.                                 scroll = c;
  193.                                 field = 0;
  194.                             }
  195.                             if (scroll > (lines - showlines)) {
  196.                                 /* if current window is last scroll window */
  197.                                 scroll = lines - showlines;
  198.                                 field = (c - (lines - showlines));
  199.                             }
  200.                         }
  201.  
  202.                         for (count = 0; count < showlines; count++) {
  203.                             locate(row+count, col-1);
  204.                             spaces = (width - strlen(prompt[count+scroll]) - 3);
  205.                             printf("│%s",prompt[count+scroll]);
  206.                             for (;(spaces); spaces--)
  207.                                 putch(' ');
  208.                         }
  209.  
  210.                         reverse(1);
  211.                         locate(row+field,col);
  212.                         printf("%s",prompt[field+scroll]);
  213.                         locate(row+field,col);
  214.                         /* return(c);  */
  215.                     }
  216.                     break;
  217.             }  /* end of normal key switch   */
  218.         else { /* get extended key  */
  219.             keycode = getch();
  220.             lastscroll = scroll;
  221.             switch(keycode) {
  222.                 case 71:
  223.                     /* -----Home   */
  224.                     reverse(0);
  225.                     locate(row+field,col);
  226.                     printf("%s",prompt[field+scroll]);
  227.                     field = 0;
  228.                     if (scroll) {
  229.                         scroll = 0;
  230.                     }
  231.                     break;
  232.                 case 73:
  233.                     /* -----Page Up   */
  234.                     reverse(0);
  235.                     locate(row+field,col);
  236.                     printf("%s",prompt[field+scroll]);
  237.                     if (field == 0)
  238.                         if (scroll >= showlines)
  239.                             scroll -= showlines;
  240.                         else scroll = 0;
  241.                     else
  242.                         field = 0;
  243.                     break;
  244.                 case 72:
  245.                     /* -----Up     */
  246.                 case 75:
  247.                     /* -----Left arrow   */
  248.                     reverse(0);
  249.                     locate(row+field,col);
  250.                     printf("%s",prompt[field+scroll]);
  251.  
  252.                     if (field)
  253.                         field--;
  254.                     else
  255.                         if (scroll)
  256.                             scroll--;
  257.                         else {
  258.                             field = showlines - 1;
  259.                             scroll = lines - showlines;
  260.                         }
  261.                     break;
  262.                 case 79:
  263.                     /* -----End    */
  264.                     reverse(0);
  265.                     locate(row+field,col);
  266.                     printf("%s",prompt[field+scroll]);
  267.  
  268.                     field = showlines - 1;
  269.                     scroll = lines - showlines;
  270.                     break;
  271.                 case 81:
  272.                     /* -----Page Down */
  273.                     reverse(0);
  274.                     locate(row+field,col);
  275.                     printf("%s",prompt[field+scroll]);
  276.                     if(field == showlines-1) {
  277.                         if (lines > (scroll + showlines))
  278.                             scroll += showlines;
  279.                         if (scroll > (lines - showlines))
  280.                             scroll = lines - showlines;
  281.                     }
  282.                     else
  283.                         field = showlines-1;
  284.                     break;
  285.                 case 80:
  286.                     /* -----Down   */
  287.                 case 77:
  288.                     /* -----Right arrow  */
  289.                     reverse(0);
  290.                     locate(row+field,col);
  291.                     printf("%s",prompt[field+scroll]);
  292.                     if (field < showlines-1)
  293.                         field++;
  294.                     else
  295.                         if (lines > showlines)
  296.                             scroll++;
  297.                         else {
  298.                             field = 0;
  299.                             scroll = 0;
  300.                         }
  301.                     if (scroll > (lines - showlines))
  302.                         /*scroll = lines - showlines;*/
  303.                         { scroll = 0;
  304.                         field = 0; }
  305.                     break;
  306.  
  307.                 default:
  308.                     ;
  309.                     /* ignore others  */
  310.             }
  311.             if (scroll != lastscroll)
  312.                 for (count = 0; count < showlines; count++) {
  313.                     locate(row+count, col-1);
  314.                     spaces = (width - strlen(prompt[count+scroll]) - 3);
  315.                     printf("│%s",prompt[count+scroll]);
  316.                     for (;(spaces); spaces--)
  317.                         putch(' ');
  318.                 }
  319.         }
  320.     }
  321. }
  322.  
  323. int sortfiles(struct find_t *items[], int count)
  324. {
  325. int i, j, value, dist;
  326. int strcmp();
  327. struct find_t *holder;
  328. struct find_t holding;
  329. holder = &holding;
  330.     /* bubble sort */
  331.     /*
  332.     for (g = 0; g < count; g++) {
  333.         for (h = g+1; h < count; h++) {
  334.             value = stricmp(items[g]->name, items[h]->name);
  335.             if (value > 0) {
  336.                 holder = items[g];
  337.                 items[g] = items[h];
  338.                 items[h] = holder;
  339.             }
  340.         }
  341.     }
  342.     */
  343.     /* shell sort  */
  344.     dist = count/2;
  345.     while (dist > 0) {
  346.         for (i=0;i<(count-dist);i++) {
  347.             j = i;   /* in loop ?? */
  348.             ;
  349.             while (j >= 0 && (value = stricmp(items[j]->name, items[j+dist]->name) > 0)) {
  350.                 holder = items[j];
  351.                 items[j] = items[j+dist];
  352.                 items[j+dist] = holder;
  353.                 j = j-dist;
  354.             }
  355.         }
  356.         dist = dist/2;
  357.     }
  358.     /* qsort */
  359.     /* qsort(items, sizeof(struct find_t), */
  360.  
  361. }
  362.  
  363.